Trò chơi 2D Cow Boy Runner

52.961 lượt xem;
1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4 using
UnityEngine.SceneManagement;
5 using
UnityEngine.UI;
6
7 public
class PlayerMoveScript : MonoBehaviour {
8
9     
public static PlayerMoveScript instance;
10
11     
public float moveSpeed,jumpForce, jumpTime, jumpTimeCounter,
12     speedMultiplier, speedIncrease, speedCount;
13
14     
public AudioSource audioSource;
15     
public AudioClip coinClip, diedClip;
16     
public Animator anim;
17     
private Rigidbody2D rigidbody;
18     
private Collider2D collider;
19
20     
public bool grounded;
21     
public LayerMask isGround;
22     
public Transform groundCheck;
23     
public float groundChecked;
24
25     
//public GameManagerScript gameManager;
26
27     
public Text scoreText;
28     
public int coinScore, score, bestScore;
29     
public float scoreCount, highScoreCount, coinCount, pointsPerSeconds;
30
31     
private bool stopJumping;
32
33     
void Start () {
34         MakeInstance ();
35         rigidbody = GetComponent<Rigidbody2D> ();
36         collider = GetComponent<Collider2D> ();
37
38         jumpTimeCounter = jumpTime;
39         speedCount = speedIncrease;
40
41         stopJumping =
true;
42     }
43
44     
void MakeInstance () {
45         
if (instance == null) {
46             instance =
this;
47         }
48     }
49
50     
void Update () {
51         
52         grounded = Physics2D.OverlapCircle(groundCheck.position, groundChecked, isGround);
53             
54         
if(transform.position.x > speedCount) {
55             speedCount += speedIncrease;
56             speedIncrease = speedIncrease * speedMultiplier;
57             moveSpeed = moveSpeed * speedMultiplier;
58         }
59
60         rigidbody.velocity =
new Vector2 (moveSpeed, rigidbody.velocity.y);
61
62         
if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) {
63
64             
if(grounded) {
65                 rigidbody.velocity =
new Vector2 (rigidbody.velocity.x, jumpForce);
66                 anim.SetTrigger (
"Jump");
67                 stopJumping =
false;
68             }
69         }
70
71         
if((Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) && !stopJumping) {
72             
if(jumpTimeCounter > 0) {
73                 rigidbody.velocity =
new Vector2 (rigidbody.velocity.x, jumpForce);
74                 jumpTimeCounter -= Time.deltaTime;
75             }
76         }
77
78         
if(Input.GetKeyUp (KeyCode.Space) || Input.GetMouseButtonUp(0)) {
79             jumpTimeCounter =
0;
80             stopJumping =
true;
81         }
82             
83         
if(grounded) {
84             jumpTimeCounter = jumpTime;
85         }
86
87
88         scoreCount += pointsPerSeconds = Time.deltaTime;
89
90         
if(scoreCount > highScoreCount) {
91             highScoreCount = scoreCount;
92
93         }
94         scoreText.text =
"Score: " + Mathf.Round(scoreCount);
95
96     }
97
98     
void OnCollisionEnter2D(Collision2D target) {
99         
if(target.gameObject.tag == "died" || target.gameObject.tag == "Crates") {
100             jumpForce =
0;
101             scoreCount =
0;
102             anim.SetTrigger (
"Died");
103             scoreText.gameObject.SetActive (
false);
104             audioSource.PlayOneShot (diedClip);
105             FindObjectOfType<GameManagerScript> ().gameOver (Mathf.RoundToInt(highScoreCount), coinScore);
106             FindObjectOfType<GameManagerScript> ().ifPlayerDiedCoinScore(coinScore);
107             FindObjectOfType<GameManagerScript> ().ifPlayerDiedScore (Mathf.RoundToInt(highScoreCount));
108         }
109     }
110
111     
void OnTriggerEnter2D(Collider2D coin) {
112         
if(coin.tag == "Coin") {
113             audioSource.PlayOneShot (coinClip);
114             coinScore++;
115             FindObjectOfType<GameManagerScript> ().SetCoinScore (coinScore);
116             coin.gameObject.SetActive (
false);
117         }
118     }
119 }


public GameManagerScript gameManager;



Gõ tìm kiếm nhanh...